package com.cq.hd.member.service.impl;

import com.cq.hd.common.enums.biz.AutoWithdrawalStateEnum;
import com.cq.hd.member.mapper.TbAutoWithdrawalRecordMapper;
import com.cq.hd.member.po.TbAutoWithdrawalRecordPo;
import com.cq.hd.member.service.AutoWithdrawalResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 自动提现结果处理逻辑
 *
 * @author much
 * @since 2024/04/27 11:30
 */
@Slf4j
@Service
public class AutoWithdrawalResultServiceImpl implements AutoWithdrawalResultService {

    @Resource
    private TbAutoWithdrawalRecordMapper autoWithdrawalRecordMapper;

    /**
     * 自动提现失败处理
     *
     * @param withdrawalId 提现记录id
     */
    @Override
    public void onFailure(Long withdrawalId) {
        updateState(withdrawalId, AutoWithdrawalStateEnum.FAILURE);
    }

    /**
     * 自动提现成功
     *
     * @param withdrawalId 提现记录id
     */
    @Override
    public void onSuccess(Long withdrawalId) {
        updateState(withdrawalId, AutoWithdrawalStateEnum.SUCCESS);
    }

    private void updateState(Long withdrawalId, AutoWithdrawalStateEnum targetState) {
        TbAutoWithdrawalRecordPo autoWithdrawalRecordPo = autoWithdrawalRecordMapper.selectByWithdrawalId(withdrawalId);
        if (autoWithdrawalRecordPo == null) {
            log.error("[自动提现处理] - 找不到对应的记录：withdrawalId={}", withdrawalId);
            return;
        }
        TbAutoWithdrawalRecordPo updater = new TbAutoWithdrawalRecordPo();
        updater.setId(autoWithdrawalRecordPo.getId());
        updater.setState(targetState.getValue());
        updater.setUpdateTime(LocalDateTime.now());
        autoWithdrawalRecordMapper.updateById(updater);
    }

}
